﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using System.ComponentModel.DataAnnotations;
using WalkingTec.Mvvm.Core;
using WalkingTec.Mvvm.Core.Extensions;
using nERP.Model.Po;
using nERP.Model;
using nERP.Model.Bas;
using nERP.Model.Fin;

namespace nERP.ViewModel.Po.BuyVMs
{
    public partial class BuyListVM : BasePagedListVM<Buy_View, BuySearcher>
    {

        protected override IEnumerable<IGridColumn<Buy_View>> InitGridHeader()
        {
            return new List<GridColumn<Buy_View>>{

                this.MakeGridHeader(x => x.Buy_BuyCode).SetTitle(@Localizer["Page.采购单号"].Value),
                this.MakeGridHeader(x => x.Buy_Supplier).SetTitle(@Localizer["Page.供应商"].Value),
                this.MakeGridHeader(x => x.Buy_InvoicesDate).SetTitle(@Localizer["Page.单据日期"].Value),
                this.MakeGridHeader(x => x.Buy_MakeUser).SetTitle(@Localizer["Page.制单人"].Value),
                this.MakeGridHeader(x => x.Buy_DeliveryDate).SetTitle(@Localizer["Page.交货日期"].Value),
                this.MakeGridHeader(x => x.Buy_Remark).SetTitle(@Localizer["_Admin.Remark"].Value),
                this.MakeGridHeader(x => x.Buy_SettlementAccount).SetTitle(@Localizer["Page.结算账户"].Value),
                this.MakeGridHeader(x => x.Buy_Status).SetTitle(@Localizer["Page.单据状态"].Value),
                this.MakeGridHeader(x => x.Buy_TotalPrice).SetTitle(@Localizer["Page.总金额"].Value),
                this.MakeGridHeader(x => x.Buy_TotalPrice1).SetTitle(@Localizer["Page.已结金额"].Value),
                this.MakeGridHeader(x => x.Buy_TotalPrice2).SetTitle(@Localizer["Page.未结金额"].Value),
                this.MakeGridHeader(x => x.Buy_CreateTime).SetTitle(@Localizer["_Admin.CreateTime"].Value),
                this.MakeGridHeader(x => x.Buy_UpdateTime).SetTitle(@Localizer["_Admin.UpdateTime"].Value),
                this.MakeGridHeader(x => x.Buy_CreateBy).SetTitle(@Localizer["_Admin.CreateBy"].Value),
                this.MakeGridHeader(x => x.Buy_UpdateBy).SetTitle(@Localizer["_Admin.UpdateBy"].Value),
                this.MakeGridHeader(x => x.Buy_IsValid).SetTitle(@Localizer["_Admin.IsValid"].Value),
                this.MakeGridHeader(x => "wtmtemp_CanApprove").SetHide().SetFormat((a, b) =>
                {
                    if(a.wtmtemp_CanApprove) { return "true"; }
                    else { return "false"; }
                }),


                this.MakeGridHeaderAction(width: 200)
            };
        }


        public override IOrderedQueryable<Buy_View> GetSearchQuery()
        {
            var query = DC.Set<Buy>()

                .CheckContain(Searcher.BuyCode, x => x.BuyCode)
                .CheckEqual(Searcher.SupplierId, x => x.SupplierId)
                .CheckBetween(Searcher.InvoicesDate?.GetStartTime(), Searcher.InvoicesDate?.GetEndTime(), x => x.InvoicesDate, includeMax: false)
                .CheckEqual(Searcher.MakeUserId, x => x.MakeUserId)
                .CheckBetween(Searcher.DeliveryDate?.GetStartTime(), Searcher.DeliveryDate?.GetEndTime(), x => x.DeliveryDate, includeMax: false)
                .CheckContain(Searcher.Remark, x => x.Remark)
                .CheckEqual(Searcher.SettlementAccountId, x => x.SettlementAccountId)
                .CheckEqual(Searcher.Status, x => x.Status)
                .CheckEqual(Searcher.TotalPrice, x => x.TotalPrice)
                .CheckEqual(Searcher.TotalPrice1, x => x.TotalPrice1)
                .CheckEqual(Searcher.TotalPrice2, x => x.TotalPrice2)
                .CheckBetween(Searcher.CreateTime?.GetStartTime(), Searcher.CreateTime?.GetEndTime(), x => x.CreateTime, includeMax: false)
                .CheckBetween(Searcher.UpdateTime?.GetStartTime(), Searcher.UpdateTime?.GetEndTime(), x => x.UpdateTime, includeMax: false)
                .CheckContain(Searcher.CreateBy, x => x.CreateBy)
                .CheckContain(Searcher.UpdateBy, x => x.UpdateBy)
                .Select(x => new Buy_View
                {
                    ID = x.ID,

                    Buy_BuyCode = x.BuyCode,
                    Buy_Supplier = DC.Set<Supplier>().Where(z0 => z0.SupplierCode == x.SupplierId).Select(y => y.SupplierName).FirstOrDefault(),
                    Buy_InvoicesDate = x.InvoicesDate,
                    Buy_MakeUser = DC.Set<FrameworkUser>().Where(z0 => z0.ITCode == x.MakeUserId).Select(y => y.Name).FirstOrDefault(),
                    Buy_DeliveryDate = x.DeliveryDate,
                    Buy_Remark = x.Remark,
                    Buy_SettlementAccount = DC.Set<SettlementAccount>().Where(z0 => z0.Code == x.SettlementAccountId).Select(y => y.Name).FirstOrDefault(),
                    Buy_Status = x.Status,
                    Buy_TotalPrice = x.TotalPrice,

                    //采购订单已结金额=付款金额+采购退货金额
                    Buy_TotalPrice1 = DC.Set<PaymentDetail>().Where(z0 => z0.BuyId == x.BuyCode && z0.Status == dStatusTypeEnum.Reviewed).Sum(z0 => z0.Amount4)
                    + DC.Set<BuyReturnDetail>().Where(z0 => z0.BuyId == x.BuyCode && z0.Status == dStatusTypeEnum.Reviewed).Sum(z0 => z0.DiscountPrice),
                    //采购订单未结金额=订单金额-已结金额
                    Buy_TotalPrice2 = x.TotalPrice - DC.Set<PaymentDetail>().Where(z0 => z0.BuyId == x.BuyCode && z0.Status == dStatusTypeEnum.Reviewed).Sum(z0 => z0.Amount4)
                    - DC.Set<BuyReturnDetail>().Where(z0 => z0.BuyId == x.BuyCode && z0.Status == dStatusTypeEnum.Reviewed).Sum(z0 => z0.DiscountPrice),

                    Buy_CreateTime = x.CreateTime,
                    Buy_UpdateTime = x.UpdateTime,
                    Buy_CreateBy = x.CreateBy,
                    Buy_UpdateBy = x.UpdateBy,
                    Buy_IsValid = x.IsValid,
                })
                .OrderBy(x => x.ID);
            return query;
        }


        public override void AfterDoSearcher()
        {
            if (Wtm.LoginUserInfo != null)
            {
                var ids = GetMyApproves();
                foreach (var item in EntityList)
                {
                    var m = ids.Find(x => x.ModelID == item.GetID().ToString());
                    if (m != null)
                    {
                        item.wtmtemp_CanApprove = true;
                    }
                }
            }
        }


    }
    public class Buy_View : Buy
    {

        public string Buy_BuyCode { get; set; }
        public string Buy_Supplier { get; set; }
        public DateTime? Buy_InvoicesDate { get; set; }
        public string Buy_MakeUser { get; set; }
        public DateTime? Buy_DeliveryDate { get; set; }
        public string Buy_Remark { get; set; }
        public string Buy_SettlementAccount { get; set; }
        public StatusTypeEnum? Buy_Status { get; set; }
        public decimal? Buy_TotalPrice { get; set; }
        public decimal? Buy_TotalPrice1 { get; set; }
        public decimal? Buy_TotalPrice2 { get; set; }
        public DateTime? Buy_CreateTime { get; set; }
        public DateTime? Buy_UpdateTime { get; set; }
        public string Buy_CreateBy { get; set; }
        public string Buy_UpdateBy { get; set; }
        public bool Buy_IsValid { get; set; }
        public bool wtmtemp_CanApprove { get; set; }

    }

}